package com.chen.demo.config;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.dfa.SensitiveUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class HutoolConfig implements InitializingBean {
    /**
     * 敏感词配置:用于初始化WordTree
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        List<String> dict = new ArrayList<>();
        URL dir = ResourceUtil.getResource("dict");
        File[] files = FileUtil.file(dir).listFiles();
        for (File file:
                files) {
            dict.addAll(new FileReader(file).readLines());
        }
        SensitiveUtil.init(dict);
    }

}
